package rc.workflow.WorkflowDefinitions;

/**
 * Created by IntelliJ IDEA.
 * User: Slejus
 * Date: 13.6.11
 * Time: 13.22
 * To change this template use File | Settings | File Templates.
 */
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.alfresco.repo.web.scripts.workflow.AbstractWorkflowWebscript;
import org.alfresco.repo.web.scripts.workflow.WorkflowModelBuilder;
import org.springframework.extensions.webscripts.Cache;
import org.springframework.extensions.webscripts.Status;
import org.springframework.extensions.webscripts.WebScriptRequest;

public class SortedWorkflowDefinitionsGet extends AbstractWorkflowWebscript{

    private final String [] workflowsSort = new String[]
    {
        "vacAppealSimple","vacAppeal","alAppeal","alAppealDep",
        "perAppealDep","perAppealDiv","perAppealDepSpec","perAppealDivSpec",
        "alNotice","alNoticeDep","alNoticeDiv","perNoticeFinDep","perNoticeFinDiv",
        "secAbroadNoticeDepSimple","secAbroadNoticeDivSimple","perNoticeOrgDepSimple","perNoticeOrgDivSimple",
        "perNoticeStructDivSimple","perNoticeStructDep","perNoticeStructDiv",
        "orderSecAbroadSimple","orderSecAbroad",
        "orderPerSimple","orderPer","perRef","appealSecLR"
    };

    @Override
    protected Map<String, Object> buildModel(WorkflowModelBuilder modelBuilder, WebScriptRequest req, Status status, Cache cache) {
        ExcludeFilter excludeFilter = null;
        String excludeParam = req.getParameter(PARAM_EXCLUDE);
        if (excludeParam != null && excludeParam.length() > 0)
        {
            excludeFilter = new ExcludeFilter(excludeParam);
        }

        // list all workflow's definitions simple representation
        List<org.alfresco.service.cmr.workflow.WorkflowDefinition> workflowDefinitions = workflowService.getDefinitions();

        // List for sorted workflow definitions by workflowSort sequence
        ArrayList<Map<String, Object>> results = new ArrayList<Map<String, Object>>();

        for (int i = 0; i < workflowsSort.length; i++){
            String SortName ="activiti$"+workflowsSort[i];
            for (org.alfresco.service.cmr.workflow.WorkflowDefinition workflowDefinition : workflowDefinitions){
                if (excludeFilter == null || !excludeFilter.isMatch(workflowDefinition.getName())){
                    if (workflowDefinition.getName().equals(SortName)){
                        results.add(modelBuilder.buildSimple(workflowDefinition));
                        break;
                    }
                }
            }
        }

        // List for unsorted workflow definitions
        ArrayList<Map<String, Object>> results2 = new ArrayList<Map<String, Object>>();

        for (org.alfresco.service.cmr.workflow.WorkflowDefinition workflowDefinition : workflowDefinitions){
            boolean isInList = false;
            for (int j = 0; j < workflowsSort.length; j++) {
                String SortName ="activiti$"+workflowsSort[j];
                if (excludeFilter == null || !excludeFilter.isMatch(workflowDefinition.getName())){
                    if (workflowDefinition.getName().equals(SortName)) {
                        isInList = true;
                        break;
                    }
                } else{
                    break;
                }
            }
            if (!isInList && (excludeFilter == null || !excludeFilter.isMatch(workflowDefinition.getName()))){
                results2.add(modelBuilder.buildSimple(workflowDefinition));
            }
        }
        results.addAll(results2);
        Map<String, Object> model = new HashMap<String, Object>();
        model.put("workflowDefinitions", results);
        return model;
    }
}